// 引入创建仓库的方法
import { createStore } from "redux";


// 初始化仓库中的数据-state
const initState = {
  name: 'Tom',
  age: 18
};
// Action Types:统一管理所有action对象的type值
const Types = {
  ADD: 'ADD',
  CHANGENAME: 'CHANGENAME'
}
// Action Creator：action的制造器，用来统一的创建action
export const actions = {
  add: ()=>({type: Types.ADD}),
  changeName: (name) => ({type: Types.CHANGENAME, name: name})
}
// 创建reducer，用来修改数据，这是一个纯函数
const reducer = (state=initState, action) => {
  switch(action.type) {
    case Types.ADD :
      return {
        ...state,
        age: state.age + 1
      }
    case Types.CHANGENAME :
      return {
        ...state,
        name: action.name
      }
    default :
     return state
  }
}

const store = createStore(reducer);
export default store;